<?php
	function authenticate($username, $password)
	{
		echo "da vao day";
		$flag = false;
		$passmd5 = md5($password);
		//echo "select IDEmployee,UserName from employee where UserName='$username' and Password = '$passmd5' and State = 1";
		$query = mysql_query("select IDEmployee,UserName,FullName,IDRole,CurrentLanguage,employee.Accountant from employee where UserName='$username' and Password = '$passmd5' and State = 1");
		
		while ($row=mysql_fetch_array($query)){
			//require_once "caching.php";
			//$cache = new SimpleCache();
			mysql_query("INSERT INTO checklogtime (IDEmployee, TimeLogIn) VALUES ('".$row['IDEmployee']."', NOW())");
			
			$flag=true;
			if($_REQUEST['check'] == "on")// Da tick
			{
				setcookie("IDEmployee",$row['IDEmployee'],time()+60*60*24*30);
		    	//setcookie("user",$row['UserName'],time()+60*60*24*30);
		    	//
		    	//setcookie("fullname",$row['FullName'],time()+60*60*24*30);
		    	//setcookie("role",$row['IDRole'],time()+60*60*24*30);
		    	//setcookie("curLanguage",$row['CurrentLanguage'],time()+60*60*24*30);
			}
		    else {
		    	setcookie('IDEmployee'); 
		    	//setcookie('user'); 
		    	//setcookie('fullname'); 
		    	//setcookie('role'); 
		    	//setcookie('curLanguage');
		    }
			$_SESSION['user'] =  $row['UserName'];
			$_SESSION['IDEmployee'] =  $row['IDEmployee'];
			$_SESSION['fullname'] =  $row['FullName'];
			$_SESSION['role'] = $row['IDRole'];
			$_SESSION['lang']= $row['CurrentLanguage'];
			$_SESSION['ketoan']=$row[Accountant];
			$lang=$_SESSION['lang'];
			
			if($lang==''){
				$lang='EN';
				$_SESSION['lang']="EN";
			}
			
			header('Location:mainPhone.php') ;
			/*if($row[IDRole]==2){
				header('Location:home.php') ;
			}else if ($row[IDRole]==1){
				header('Location:homeAdmin.php');
			}else{
				header('Location:partnermanage.php');
			}*/
			//echo "<meta http-equiv='refresh' content='0; url=menu.php' /> ";
		}		
		if($flag==false){
			
			$_SESSION['error']="";
			header( 'Location: login.php' );
			//echo "<meta http-equiv='refresh' content='0; url=login.php' /> ";
		}
	}
?>